package com.alibaba.sdk.controller;

import com.alibaba.sdk.constant.UserInfoConstant;
import com.alibaba.sdk.model.OperationResult;
import com.alibaba.sdk.model.dao.UserMapper;
import com.alibaba.sdk.model.dao.UserTypeMapper;
import com.alibaba.sdk.model.entity.User;
import com.alibaba.sdk.model.entity.UserType;
import com.alibaba.sdk.model.input.OneUserInput;
import com.alibaba.sdk.model.input.UserAddInput;
import com.alibaba.sdk.model.input.UserListInput;
import com.alibaba.sdk.model.input.UserTypeInput;
import com.alibaba.sdk.model.output.OneUserResponse;
import com.alibaba.sdk.model.output.UserAddResponse;
import com.alibaba.sdk.model.output.UserListResponse;
import com.alibaba.sdk.service.UserService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 用户相关接口
 *
 * @ writer Shawn(Shawn_0217@163.com)
 * @ date & time 2021/4/26 22:59
 * @ company alibaba
 */
@Api(tags = "用户相关接口")
@RequestMapping("/user")
@RestController
public class UserController {

    @Resource
    private UserService userService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserTypeMapper userTypeMapper;

    /**
     * 用户列表翻页查询
     *
     * @param userListInput
     * @return
     */
    @ApiOperation("用户列表分页查询")
    @GetMapping("/list")
    @RequiresRoles(value = {UserInfoConstant.SUPER_ADMIN, UserInfoConstant.ADMIN}, logical = Logical.OR)
    public OperationResult<UserListResponse> userList(UserListInput userListInput) {
        return userService.userList(userListInput);
    }

    /**
     * 用户新增
     *
     * @param userAddInput
     * @return
     */
    @ApiOperation("用户新增")
    @PostMapping("/add")
    @RequiresRoles(value = {UserInfoConstant.SUPER_ADMIN})
    public OperationResult<UserAddResponse> userAdd(@Valid @RequestBody UserAddInput userAddInput) {
        return userService.userAdd(userAddInput);
    }

    /**
     * 查看单条用户信息
     *
     * @param input
     * @return
     */
    @ApiOperation("查看单条用户信息")
    @GetMapping("/getOne")
    @RequiresRoles(value = {UserInfoConstant.SUPER_ADMIN, UserInfoConstant.ADMIN}, logical = Logical.OR)
    public OperationResult<OneUserResponse> getOne(@Valid OneUserInput input) {
        return userService.getOne(input);
    }

    /**
     * 切换从数据源
     *
     * @return
     */
    @DS("slave")
    @GetMapping("/getTypes")
    public OperationResult getTypes() {
        return OperationResult.success(userTypeMapper.selectList(new LambdaQueryWrapper<>()));
    }

    @PostMapping("/get")
    @RequiresRoles(value = {UserInfoConstant.SUPER_ADMIN, UserInfoConstant.ADMIN}, logical = Logical.OR)
    public OperationResult<User> get(@RequestBody @Valid UserTypeInput input) {
        return userService.get(input);
    }
}
